package com.cn.action;

import java.util.Iterator;
import java.util.Set;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.cn.dao.OrderXiangmuDao;
import com.cn.dao.OrdersDao;
import com.cn.dao.TaocanAddDao;
import com.cn.dao.TaocanDao;
import com.cn.dao.TiJianXiangmuDao;
import com.cn.dao.UserDao;
import com.cn.entity.Order_xiangmu;
import com.cn.entity.Orders;
import com.cn.entity.Taocan;
import com.cn.entity.Taocan_xiangmu;
import com.cn.entity.TiJianxiangmu;
import com.cn.entity.User;
import com.opensymphony.xwork2.Action;

public class OrderAddAction implements Action {
      
    
    User user;
    
	String xiangmuming;
	String taocanming;

	
	public User getUser() {
		return user;
	}



	public void setUser(User user) {
		this.user = user;
	}




	public String getXiangmuming() {
		return xiangmuming;
	}



	public void setXiangmuming(String xiangmuming) {
		this.xiangmuming = xiangmuming;
	}







	public String getTaocanming() {
		return taocanming;
	}







	public void setTaocanming(String taocanming) {
		this.taocanming = taocanming;
	}







	







	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
	    double price=0;
		UserDao ud=new UserDao();
		
		  ud.add(user);
		  
		int userid=user.getUserid();
		
		
		Orders order=new Orders();
		
		order.setUserid(userid);
		
		order.setDate("2013-7-17");
		
		OrdersDao od=new OrdersDao();
		od.add(order);
		
		
		int orderid=order.getOrderid();
		String[] values = ServletActionContext.getRequest().getParameterValues("xiangmuming");
		  if(values!=null) {
		
		 for (int i=0;i<values.length;i++){
	        	String id=values[i];
	        	int xiangmuid=Integer.parseInt(id);
	        	
	        	Order_xiangmu ox=new Order_xiangmu(orderid,xiangmuid);
	        	OrderXiangmuDao oxd=new OrderXiangmuDao();
	        	oxd.add(ox);
	        	 
	        	TiJianXiangmuDao tj=new TiJianXiangmuDao();
	        	TiJianxiangmu tx=tj.scanOne(xiangmuid);
	        	
	        	price=price+tx.getPrice();
	        	
	        }
		  }
		
		 
		 
		 String[] value = ServletActionContext.getRequest().getParameterValues("taocanming");
		 if(value!=null){
		 
		 for (int i=0;i<value.length;i++){
	        	String id=value[i];
	        	
	        	int taocanid=Integer.parseInt(id);
	        	
	        	TaocanDao td=new TaocanDao();
	        	Taocan taocan=td.scanOne(taocanid);
	        	price=price+taocan.getPrice();
	        	Set<TiJianxiangmu> xiangmu=taocan.getXiangmu();
	        	Iterator<TiJianxiangmu> it=xiangmu.iterator();

	        	while(it.hasNext()){
	        		TiJianxiangmu xm=it.next(); 
	        		int xiangmuid=xm.getXiangmuid();
	        
	        	
	        	
	        	Order_xiangmu ox=new Order_xiangmu(orderid,xiangmuid);
	        	OrderXiangmuDao oxd=new OrderXiangmuDao();
	        	oxd.add(ox);}
	        	        	      	
	        	
	        }
		 }
		
		HttpSession session=ServletActionContext.getRequest().getSession();	
		session.setAttribute("orderid", orderid);
		session.setAttribute("price", price);
		
		
		return "success";
		
		
		
	}
	
	
	
}
